﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Runtime.CompilerServices;

using ConstructionBidOnline.DAL;
using ConstructionBidOnline.DAL.dsLicenseTableAdapters;
using ConstructionBidOnline.Common;

namespace ConstructionBidOnline.Biz
{
    /// <summary>
    /// License Business Object
    /// </summary>

    public class License : BOBase
    {
        #region Construction / Disposal

        /// <summary>
        /// Default constructor.
        /// </summary>
        protected License() { }

        #endregion

        #region Public Properties

        #region Instance Properties


        /// <summary>
        /// LicenseID.
        /// </summary>
        public int LicenseID
        {
            get { return _licenseID; }
            set
            {
                if (_licenseID != value)
                {
                    if (OnPropertyChanging("LicenseID", _licenseID, value))
                    {
                        int oldValue = _licenseID;
                        _licenseID = value;
                        OnPropertyChanged("LicenseID", oldValue, value);
                    }
                }
            }
        }  private int _licenseID = -1;


        /// <summary>
        /// UserID.
        /// </summary>
        public int UserID
        {
            get { return _userID; }
            set
            {
                if (_userID != value)
                {
                    if (OnPropertyChanging("UserID", _userID, value))
                    {
                        int oldValue = _userID;
                        _userID = value;
                        OnPropertyChanged("UserID", oldValue, value);
                    }
                }
            }
        }  private int _userID = -1;


        /// <summary>
        /// LicenseNumber.
        /// </summary>
        public string LicenseNumber
        {
            get { return _licenseNumber; }
            set
            {
                if (_licenseNumber != value)
                {
                    if (OnPropertyChanging("LicenseNumber", _licenseNumber, value))
                    {
                        string oldValue = _licenseNumber;
                        _licenseNumber = value;
                        OnPropertyChanged("LicenseNumber", oldValue, value);
                    }
                }
            }
        }  private string _licenseNumber = null;



        /// <summary>
        /// LicenseCategory.
        /// </summary>
        public string LicenseCategory
        {
            get { return _licenseCategory; }
            set
            {
                if (_licenseCategory != value)
                {
                    if (OnPropertyChanging("LicenseCategory", _licenseCategory, value))
                    {
                        string oldValue = _licenseCategory;
                        _licenseCategory = value;
                        OnPropertyChanged("LicenseCategory", oldValue, value);
                    }
                }
            }
        }  private string _licenseCategory = null;



        /// <summary>
        /// LicenseType.
        /// </summary>
        public string LicenseType
        {
            get { return _licenseType; }
            set
            {
                if (_licenseType != value)
                {
                    if (OnPropertyChanging("LicenseType", _licenseType, value))
                    {
                        string oldValue = _licenseType;
                        _licenseType = value;
                        OnPropertyChanged("LicenseType", oldValue, value);
                    }
                }
            }
        }  private string _licenseType = null;


        /// <summary>
        /// TradeLicenseLocale.
        /// </summary>
        public string TradeLicenseLocale
        {
            get { return _tradeLicenseLocale; }
            set
            {
                if (_tradeLicenseLocale != value)
                {
                    if (OnPropertyChanging("TradeLicenseLocale", _tradeLicenseLocale, value))
                    {
                        string oldValue = _tradeLicenseLocale;
                        _tradeLicenseLocale = value;
                        OnPropertyChanged("TradeLicenseLocale", oldValue, value);
                    }
                }
            }
        }  private string _tradeLicenseLocale = null;


        /// <summary>
        /// TradeLicenseAuthority.
        /// </summary>
        public string TradeLicenseAuthority
        {
            get { return _tradeLicenseAuthority; }
            set
            {
                if (_tradeLicenseAuthority != value)
                {
                    if (OnPropertyChanging("TradeLicenseAuthority", _tradeLicenseAuthority, value))
                    {
                        string oldValue = _tradeLicenseAuthority;
                        _tradeLicenseAuthority = value;
                        OnPropertyChanged("TradeLicenseAuthority", oldValue, value);
                    }
                }
            }
        }  private string _tradeLicenseAuthority = null;


        /// <summary>
        /// Insured.
        /// </summary>
        public bool Insured
        {
            get { return _insured; }
            set
            {
                if (_insured != value)
                {
                    if (OnPropertyChanging("Insured", _insured, value))
                    {
                        bool oldValue = _insured;
                        _insured = value;
                        OnPropertyChanged("Insured", oldValue, value);
                    }
                }
            }
        }  private bool _insured = false;



        /// <summary>
        /// Bounded.
        /// </summary>
        public bool Bounded
        {
            get { return _bounded; }
            set
            {
                if (_bounded != value)
                {
                    if (OnPropertyChanging("Bounded", _bounded, value))
                    {
                        bool oldValue = _bounded;
                        _bounded = value;
                        OnPropertyChanged("Bounded", oldValue, value);
                    }
                }
            }
        }  private bool _bounded = false;


        /// <summary>
        /// LicenseIssuingDate.
        /// </summary>
        public DateTime? LicenseIssuingDate
        {
            get { return _licenseIssuingDate; }
            set
            {
                if (_licenseIssuingDate != value)
                {
                    if (OnPropertyChanging("LicenseIssuingDate", _licenseIssuingDate, value))
                    {
                        DateTime? oldValue = _licenseIssuingDate;
                        _licenseIssuingDate = value;
                        OnPropertyChanged("LicenseIssuingDate", oldValue, value);
                    }
                }
            }
        }  private DateTime? _licenseIssuingDate = null;


        /// <summary>
        /// LicenseExpiryDate.
        /// </summary>
        public DateTime? LicenseExpiryDate
        {
            get { return _licenseExpiryDate; }
            set
            {
                if (_licenseExpiryDate != value)
                {
                    if (OnPropertyChanging("LicenseExpiryDate", _licenseExpiryDate, value))
                    {
                        DateTime? oldValue = _licenseExpiryDate;
                        _licenseExpiryDate = value;
                        OnPropertyChanged("LicenseExpiryDate", oldValue, value);
                    }
                }
            }
        }  private DateTime? _licenseExpiryDate = null;



        /// <summary>
        /// LicenseDescription.
        /// </summary>
        public string LicenseDescription
        {
            get { return _licenseDescription; }
            set
            {
                if (_licenseDescription != value)
                {
                    if (OnPropertyChanging("LicenseDescription", _licenseDescription, value))
                    {
                        string oldValue = _licenseDescription;
                        _licenseDescription = value;
                        OnPropertyChanged("LicenseDescription", oldValue, value);
                    }
                }
            }
        }  private string _licenseDescription = null;



        #endregion

        #endregion

        #region Public Methods

        #region CreateNew

        /// <summary>
        /// Creates a new, empty License object.
        /// </summary>
        public static License CreateNew()
        {
            License obj = new License();

            // Switch state to Added prior to initializing property values.
            obj.ObjState = ObjectState.Added;

            return (obj);
        }


        [MethodImpl(MethodImplOptions.Synchronized)]
        public static License CreateNew(LicenseCollection collection)
        {
            License obj = CreateNew();
            collection.Add(obj);
            return (obj);
        }

        #endregion

        #region GetLicenseByID

        /// <summary>
        /// Returns a collection of License.
        /// </summary>
        public static License GetLicenseByID(int Licenseid)
        {
            LicensesTableAdapter adapter = new LicensesTableAdapter();
            adapter.Connection = AppSettings.GetDbConn();

            dsLicense.LicensesDataTable tbl = adapter.GetData(Licenseid);

            if (tbl.Rows.Count == 1)
            {
                License license = new License();
                RelationalToObject(license, tbl.Rows[0]);
                return (license);
            }
            else if (tbl.Rows.Count > 1)
                throw new ApplicationException("Database integrity error: Specified 'Licenseid' returned more than one User record.");

            return (null);
        }

        #endregion

        #region GetLicenseByUserID

        /// <summary>
        /// Returns a collection of License for the specified 'LicenseID' or null if no License with the specified parameter(s) exists.
        /// </summary>
        public static LicenseCollection GetLicenseByUserID(int userid)
        {
            LicensesTableAdapter adapter = new LicensesTableAdapter();
            adapter.Connection = AppSettings.GetDbConn();

            dsLicense.LicensesDataTable tbl = adapter.GetDataByUserID(userid);

            LicenseCollection list = new LicenseCollection();

            if (tbl.Rows.Count > 0)
            {
                foreach (dsLicense.LicensesRow row in tbl.Rows)
                {
                    License license = new License();
                    RelationalToObject(license, row);
                    list.Add(license);
                }
            }

            return (list);
        }

        #endregion

        #region GetCertificateByLicenseNumber

        /// <summary>
        /// Returns a collection of License.
        /// </summary>
        public static License GetCertificateByLicenseNumber(string LicenseNumber)
        {
            LicensesTableAdapter adapter = new LicensesTableAdapter();
            adapter.Connection = AppSettings.GetDbConn();

            dsLicense.LicensesDataTable tbl = adapter.GetDataByLicenseNumber(LicenseNumber);

            if (tbl.Rows.Count == 1)
            {
                License license = new License();
                RelationalToObject(license, tbl.Rows[0]);
                return (license);
            }
            else if (tbl.Rows.Count > 1)
                throw new ApplicationException("Database integrity error: Specified 'LicenseNumber' returned more than one User record.");

            return (null);
        }

        #endregion

        #region Update

        /// <summary>
        /// Updates the specified License.
        /// </summary>
        public static void Update(License obj)
        {
            bool updateLicense = false;

            // Do not update invalid objects.
            if (!obj.Validate())
            {
                StringBuilder sb = new StringBuilder(2048);
                sb.Append("The object being updated has one or more invalid properties; the Update() operation for this object has been cancelled.\nError(s):");
                foreach (ValidationError err in obj.Errors)
                    sb.Append("\n* " + err.Element + ": " + err.Message);

                throw new InvalidObjectException(sb.ToString());
            }
            else
            {
                updateLicense = (obj.ObjState != ObjectState.Unchanged);

                if (!updateLicense)
                    return;
            }

            ObjectState originalObjectState = obj.ObjState;

            dsLicense.LicensesDataTable tbl = null;
            if (updateLicense)
            {
                LicensesTableAdapter adapter = new LicensesTableAdapter();
                adapter.Connection = AppSettings.GetDbConn();


                tbl = new dsLicense.LicensesDataTable();
                dsLicense.LicensesRow row = tbl.NewLicensesRow();
                ObjectToRelational(obj, tbl, row);

                adapter.Update(tbl);

                if (originalObjectState == ObjectState.Deleted)
                    obj = null;
                else RelationalToObject(obj, tbl.Rows[0]);
            }
        }

        #endregion

        #region Validate

        /// <summary>
        /// Validates the object based on its properties at the time of the call; updates the Errors property.
        /// </summary>
        public override bool Validate()
        {
            Errors.Clear();

            if (ObjState == ObjectState.Unchanged)
            {
                // By definition, unchanged objects are valid.
                return (true);
            }
            else if (ObjState == ObjectState.Uninitialized)
            {
                Errors.Add(new ValidationError("object", "Object is uninitialized.", ValidationError.Severity.Error));

                // No need to proceed past this error.
                return (false);
            }
            else if ((ObjState == ObjectState.Deleted))
            {
                // Deleted items must have 'real' (i.e. non-negative, non-zero) key(s) to be valid.
                Errors.Add(new ValidationError("LicenseID", "Deleted object has an invalid PKID.", ValidationError.Severity.Error));

                // No need to proceed past this error.
                return (false);
            }
            else  // ObjState == Added | Modified
            {

            }

            return (Errors.Count == 0);
        }

        #endregion

        #region ToString

        /// <summary>
        /// User-friendly string represetation of this object.
        /// </summary>
        public override string ToString()
        {
            return (String.Format("License: LicenseID={0}, LicenseNumber={1}, {2}", LicenseID, LicenseNumber));
        }

        #endregion

        #endregion

        #region Privates


        #endregion

    }

    /// <summary>
    /// Collection of 0 or more SubAdmin objects.
    /// </summary>

    public class LicenseCollection : List<License>
    {
        #region Types

        public class UserComparer : IComparer<License>
        {
            public int Compare(License l1, License l2)
            {
                return (String.Compare(l1.LicenseID.ToString(), l2.LicenseID.ToString()));
            }
        }

        #endregion
    }
}
